package com.curtain.koreyoshi.view;


import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.curtain.koreyoshi.bean.AdData;
import com.curtain.koreyoshi.imgload.ImageLoader;
import com.curtain.koreyoshi.utils.ResUtil;
import com.curtain.koreyoshi.view.widget.AdChargeContentView;
import com.curtain.koreyoshi.view.widget.VerticalLock;


/**
 * Created by leejunpeng on 2016/4/5.
 */
public class AdChargeView extends AdBaseView {
    private static final String TAG = AdChargeView.class.getSimpleName();

    private AdChargeContentView adChargeContentView;
    private VerticalLock verticalLock;

    public AdChargeView(Context context, AdData adData) {
        super(context, adData);
    }

    public AdChargeView(Context context, AdData adData,float battery) {
        super(context, adData,battery);
    }

    @Override
    public void initView() {
        FrameLayout frameLayout = new FrameLayout(mContext);
        LinearLayout backLayout = new LinearLayout(mContext);
        backLayout.setOrientation(LinearLayout.VERTICAL);
        LinearLayout topLayout = new LinearLayout(mContext);
        GradientDrawable topBackground = new GradientDrawable();
//        topBackground.setColor(Color.parseColor("#2f7eff"));
        topBackground.setColor(Color.parseColor("#23a4ff"));
        topLayout.setBackgroundDrawable(topBackground);
        LayoutParams topParam = new LayoutParams(LayoutParams.MATCH_PARENT,0,2);
        LinearLayout bottomLayout = new LinearLayout(mContext);
        GradientDrawable bottomBackground = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,new int[]{Color.parseColor("#ff3996"),Color.parseColor("#02dda7")});
        bottomBackground.setGradientType(GradientDrawable.LINEAR_GRADIENT);
        bottomLayout.setBackgroundDrawable(bottomBackground);
        LayoutParams bottomParam = new LayoutParams(LayoutParams.MATCH_PARENT,0,3);
        backLayout.addView(topLayout,topParam);
        backLayout.addView(bottomLayout,bottomParam);

        LinearLayout mainLayout = new LinearLayout(mContext);
        mainLayout.setOrientation(LinearLayout.VERTICAL);
        FrameLayout.LayoutParams mainParams = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        LayoutParams framParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        mainParams.gravity = Gravity.CENTER_HORIZONTAL;
        LayoutParams emptyParams = new LayoutParams(LayoutParams.MATCH_PARENT,0,35);
        LinearLayout empty = new LinearLayout(mContext);
        mainLayout.addView(empty,emptyParams);

        adChargeContentView = new AdChargeContentView(mContext);
        LayoutParams adParams = new LayoutParams(LayoutParams.MATCH_PARENT,0,40);
        adParams.leftMargin = ResUtil.widthDip2px(32);
        adParams.rightMargin = ResUtil.widthDip2px(32);
        adParams.bottomMargin = ResUtil.heightDip2px(32);
        mainLayout.addView(adChargeContentView,adParams);

        verticalLock = new VerticalLock(mContext);
        LayoutParams lockParams = new LayoutParams(LayoutParams.MATCH_PARENT,0,55);
        mainLayout.addView(verticalLock,lockParams);

        frameLayout.addView(backLayout,mainParams);
        frameLayout.addView(mainLayout,mainParams);

        this.addView(frameLayout,framParams);
    }

    @Override
    public void initData() {
        mImageLoader.DisplayImage(mAdData.getIconImageUrl(),adChargeContentView.iv_ad_icon,true);
        adChargeContentView.tv_ad_title.setText(mAdData.getTitle());
        adChargeContentView.tv_ad_desc.setText(mAdData.getMainContent());
        adChargeContentView.btn_cancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                adChargeContentView.iv_ad_icon.setImageBitmap(null);
                ImageLoader.recycleImageView(adChargeContentView.iv_ad_icon);
                if (mOnAdClickListener != null ) {
                    mOnAdClickListener.onAdRemoved(mAdData);
                }
            }
        });
        adChargeContentView.btn_install.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                adChargeContentView.iv_ad_icon.setImageBitmap(null);
                ImageLoader.recycleImageView(adChargeContentView.iv_ad_icon);
                if (mOnAdClickListener != null ) {
                    mOnAdClickListener.onAdClicked(mAdData);
                }
            }
        });
        verticalLock.topImageView.setBattery(battery);
        verticalLock.setUnlockListener(new VerticalLock.UnlockListener() {
            @Override
            public void unlockSuccess() {
                adChargeContentView.iv_ad_icon.setImageBitmap(null);
                ImageLoader.recycleImageView(adChargeContentView.iv_ad_icon);
                if (mOnAdClickListener != null) {
                    mOnAdClickListener.onAdRemoved(mAdData);
                }
            }

            @Override
            public void unlockFailure() {

            }
        });
    }
}
